package com.yuexun.beilunpatient.ui.question.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.question.adapter.QuestionListAdapter;
import com.yuexun.beilunpatient.ui.question.bean.PatientQuestionBean;
import com.yuexun.beilunpatient.ui.question.model.impl.QuestionModel;
import com.yuexun.beilunpatient.ui.question.presenter.impl.PatientQuestionPresenter;
import com.yuexun.beilunpatient.ui.question.ui.Act_Question_Detail;
import com.yuexun.beilunpatient.ui.question.ui.view.IPatientQuestionView;
import com.yuexun.beilunpatient.widget.EmptyLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/3/22 0022.
 */

public class Frag_Answered extends KJFragment implements ICallBackListener, IPatientQuestionView {
    @Bind(R.id.content_list)
    PullToRefreshList contentList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private ListView mList;
    QuestionListAdapter adapter;
    PatientQuestionPresenter presenter;
    List<PatientQuestionBean> questionBeans = new ArrayList<>();
    private int nowpage = 1;
    private int pagesize = 20;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_list, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        presenter = new PatientQuestionPresenter(new QuestionModel(), this);
        listViewPreference();
//        initPresenter();
    }

    private void initPresenter(){
        Map<String, String> params = new HashMap<>();
        params.put("myQuestionStatus", "2");
        params.put("status", "1");//0未答复 ，1已答复
        params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", nowpage + "");
        params.put("pageSize", pagesize + "");
        presenter.inquirePatientQuestionListPage(params);
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = contentList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        contentList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) contentList.getFooterLoadingLayout())
                .setNoMoreDataText("已经是最新状态");

        adapter = new QuestionListAdapter(mList, questionBeans, R.layout.item_question);
        mList.setAdapter(adapter);

        emptyLayout.dismiss();

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Bundle bundle2 = new Bundle();
                Intent intent = new Intent(getActivity(), Act_Question_Detail.class);
                bundle2.putSerializable("questionDtl", questionBeans.get(position));
                intent.putExtras(bundle2);
                startActivity(intent);
            }
        });
        adapter.setBackListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {

            }
        });

        contentList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                initPresenter();
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                initPresenter();
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                initPresenter();
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            nowpage = 1;
            initPresenter();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        nowpage = 1;
        initPresenter();
    }

    @Override
    public void callback(Bundle bundle) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void showPatientQuestion(BaseEntity<PatientQuestionBean> entity) {
        if (contentList.isPullRefreshEnabled()) {
            contentList.onPullDownRefreshComplete();
        }
        if (entity.getStatus().equals("200")) {
            if (entity.getDatas().size() == 0) {
                contentList.setHasMoreData(false);
            } else {
                if (contentList.isPullLoadEnabled()) {
                    contentList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                questionBeans.clear();
                questionBeans = entity.getDatas();
            } else {
                questionBeans.addAll(entity.getDatas());
            }
            adapter.refresh(questionBeans);
        }else {
            ViewInject.toast(entity.getMessage());
        }

        if (questionBeans.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
